from threading import Thread
from task_queue import MyQueue
import time
import smtplib
from email.mime.text import MIMEText
from email.header import Header

# 继承thread类
class SendEmail(Thread):
    def __init__(self,email):
        super().__init__()
        self.mail_host = "smtp.qq.com"
        self.mail_user = '347155713@qq.com'
        self.mail_pass = 'oowgpztkfebkbjed'

        self.sender = '347155713@qq.com' # 发送方
        self.receivers = [email]
        self.message = MIMEText('节日祝福', _charset='utf-8')
        self.message["From"] = Header("祝你开心", 'utf-8')
        self.message["To"] = Header("测试", 'utf-8')
        self.subject = '哈哈哈哈哈'
        self.message["Subject"] = Header(self.subject, 'utf-8')

    def run(self):
        try:
            smtobj = smtplib.SMTP()
            smtobj.connect(self.mail_host, 25)
            smtobj.login(user=self.mail_user, password=self.mail_pass)
            smtobj.sendmail(self.sender, self.receivers, self.message.as_string())
        except Exception as e:
            print(e)



def main():
    q = MyQueue("myqueue", decode_responses=True)

    while 1:
        # print(q.peek())
        print(q.empty())

        if not q.empty():
            break
        for i in range(3):
            # print(q.peek())
            se = SendEmail(q.peek())
            se.start()

if __name__ == '__main__':
    a = time.time()
    main()
    b = time.time()
    print(b-a)

